/**
 * 文件：activity_cover.java
 * 包名：com.ronri.ams.sample;   --- 正式发布时需要修正
 * 说明：启动封面
 * 作者：MS During
 * 日期：2017/5/14
 * 历史：
 */
package com.ronri.ams;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.application.InitApplication;

import java.util.Timer;
import java.util.TimerTask;

public class activity_cover extends Activity {

    // ***
    private Timer timer = null;

    private ImageView mBackground = null;

    // ***
    private int index = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置无标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //设置全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_cover);

        mBackground = (ImageView)findViewById(R.id.backImage);

        mBackground.setImageResource(R.drawable.welcome);
        // 创建数据库
        SystemInit.InitDB(this);

        timer = new Timer(true);
        timer.schedule(hello, 200, 1000);
    }

    TimerTask hello = new TimerTask(){
        @Override
        public void run() {
            if(index < 3){
                index++;
            }else{

                //停止播放，启动主Activity
                timer.cancel();
                finish();
                //不加这一句，按回退键就会回到欢迎界面不合理。

                SharedPreferences pref = getSharedPreferences("first", MODE_PRIVATE);
                SharedPreferences.Editor editor = pref.edit();
                boolean bFirst = pref.getBoolean("status",true);

                /// bFirst = true;

                if (bFirst){
                    Intent intent = new Intent(InitApplication.getContext(), com.ronri.guide.GuideActivity.class);
                    startActivity(intent);
                    // 设置标志位，避免下次再次调用
                    editor.putBoolean("status", false);
                    editor.apply();
                }else{
                    Intent intent = new Intent(InitApplication.getContext(), activity_login.class);
                    startActivity(intent);
                }
            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();

        Drawable drawable= mBackground.getDrawable();
        if(drawable instanceof BitmapDrawable){
            Bitmap bmp = ((BitmapDrawable)drawable).getBitmap();
            if (bmp != null && !bmp.isRecycled()){
                mBackground.setImageBitmap(null);
                bmp.recycle();
                bmp=null;
            }
        }

        // 清空后台
        setContentView(R.layout.s_free_layout);

        timer = null;
        mBackground = null;

        System.gc();
        System.runFinalization();
    }
}
